#include <avz.h>
#include "helium/helium_helper_se.h"
#include "SimpleAvZ/lib.h"

using namespace AvZ;

void C_5(Time time, ShovelTime shovel_time)
{
    C(time, shovel_time, {PUFF, SUN, SCAREDY, POT, SUNFLOWER}, {1, 2, 3, 4, 5}, 9);
}

void PD_410_down()
{
    P(410, 5, 8.575);
    D(after(220), 4, 8.575);
}
void PD_410_up()
{
    P(410, 2, 8.575);
    D(after(220), 1, 8.575);
}
void Pd_581_up()
{
    P(581, 2, 9);
    D(after(360), 2, 4);
}
void Pd_581_down()
{
    P(581, 4, 9);
    D(after(360), 4, 4);
}
void Pd_594_up()
{
    P(594, 2, 8.625);
    D(after(374), 2, 3.8);
}
void Pd_594_down()
{
    P(594, 4, 8.625);
    D(after(374), 4, 3.8);
}

void Script()
{
    SeedSlot(10);                                    // 10 卡槽
    OpenMultipleEffective('Q', MAIN_UI_OR_FIGHT_UI); // 反复重开
    SetAdvancedPauseKey('Z');                        // 高级暂停
    SetSkipTickKey('S', 9);                          // 跳帧
    SetSkipTickKey('D', 19);
    SetErrorMode(NONE);

    int length = 1507;

    // 测试用
    // SetErrorMode(CONSOLE);
    // SetZombies({TT_18});
    // SetRowProperty(0, 0, 1, 0, 0, 0);
    // SetWavelength({{1, 601}, {2, 601}, {3, 1530}, {4, 1498}, {5, 1498}, {6, 1323}, {7, 1490}, {8, 1490}, {10, 680}, {11, 746}, {12, 1243}, {13, 1486}, {14, 746}, {15, 1243}, {16, 1486}, {17, 746}, {18, 1813}});
    // SetGameSpeed(2);

    SetZombies_AllType(DE);
    SelectCards({
        ICE_SHROOM,     // I
        M_ICE_SHROOM,   // I'
        COFFEE_BEAN,    // 咖啡豆
        CHERRY_BOMB,    // A
        JALAPENO,       // A'
        PUFF_SHROOM,    // c
        SUN_SHROOM,     // c
        SCAREDY_SHROOM, // c
        FLOWER_POT,     // c
        SUNFLOWER,      // c
    });

    SetTime(-599, 1);
    AvZ::MaidCheats::dancing();

    SetIce({{1, 6}, {2, 6}, {3, 6}, {4, 6}, {5, 6}, {5, 3}});

    for (auto w : waves(1))
    {
        // PPDDA'+5c
        PP(225);
        DD(after(107), 8.175);
        C(401, JALAPENO, 3, 9);
        C_5(599, until(601 + 341 - 207));

        EnsureExist({GIGA, 3});
    }

    for (auto w : waves(2))
    {
        // PPSADD
        PP(291);
        P(291, 2, 9);
        C(294, CHERRY, 4, 9);
        DD(401, 8.8);
        RM(193, 4, 9); // 防止留存花盆导致樱桃瞬爆

        I(601);
    }

    for (auto w : waves(3))
    {
        // IPP+c+c-PP 1530
        PP(301, {1, 4}, 9); // 302冰车碾9小喷
        C_5(148, keep(751));
        C_5(after(751), until(1265)); // 最早434*2+399=1267举锤
        DD(1330, 8.8);
        RM(1330 - 181 - 266, 1, 9); // 提前铲垫防止漏撑杆

        I(1530 + 10);

        EnsureExist({POGO_ZOMBIE, 3});
    }

    for (auto w : waves(4))
    {
        // I3PPdd-PP 1507
        DD(594, 8.625);
        DD(after(374), {2, 4}, 3.825);
        C_5(800, keep(266)); // 1223-181-266=776
        PP(length - 200, 8.625);

        I(length + 10);
    }

    for (auto w : waves(5))
    {
        // I3PD/Pd-DD
        PD_410_up();
        Pd_581_down();
        C_5(800, keep(266));
        DD(length - 200, 8.625);

        I(length + 10);

        EnsureExist({GIGA, 2});
    }

    for (auto w : waves(6, 12))
    {
        // I3A'AD/Pd-PP
        C(366 - 107, JALAPENO, 1, 9);
        C(366, CHERRY, 1, 9);
        D(622, 1, 8.1);
        Pd_581_down();
        C_5(800, keep(266));
        PP(length - 200, 8.625);

        I(length + 10);

        EnsureExist({DIGGER_ZOMBIE, 2});
    }

    /*for (auto w : waves(7, 17))
    {
        // I3PPDD-PP
        PD_410_up();
        PD_410_down();
        C_5(802, keep(266));
        PP(length - 200, 8.8);

        I(length + 10);
    }*/

    for (auto w : waves(7, 8, 13, 14, 15, 16, 17, 18))
    {
        // I3PD/Pd-PP
        PD_410_up();
        Pd_581_down();
        C_5(800, keep(266));
        PP(length - 200, 8.8);

        I(length + 10);
    }

    for (auto w : waves(9, 19))
    {
        // I3PD/Pd-PDD/PADA'
        PD_410_up();
        Pd_581_down();
        C_5(800, keep(266));
        PP(1206, 8.6);
        C(1416, CHERRY, 4, 9); // 最早举锤445*2-10+399=1279
        D(1416, 2, 9);
        DD(after(374), 4.2625);
        C(1416 + 414 + 212, JALAPENO, 4, 9);
        RM(after(-101), 4, 9);

        C(800 + 751, keep(751), {PUFF, SUN, SCAREDY, POT}, {1, 2, 4, 5}, 9);
        C(after(751), keep(751), {PUFF, SUN, SCAREDY, POT}, {1, 2, 4, 5}, 9);
        C(after(134), keep(134), SUNFLOWER, 5, 9);

        PP(2549);
        P(2598, 2, 9);
        P(2923, 4, 9);

        SetIce(0, {{1, 6}, {2, 6}, {3, 6}, {4, 6}, {3, 9}});
        EnsureExist({GIGA, 1, 2, 4, 5});
    }

    for (auto w : waves(10))
    {
        // PPDDdd
        PP(225);
        PP(330);
        DD(after(227), 4.5);

        I(601);

        EnsureExist({POGO_ZOMBIE, 1});
        EnsureExist({POLE_VAULTING_ZOMBIE, 3});
    }

    for (auto w : waves(11))
    {
        // IPP-PP
        PP(301, {2, 4}, 9); // 冰车碾9小喷
        C_5(148, keep(751));
        C_5(after(751), until(1307 - 181 - 1)); // 最早434*2+399=1267举锤
        DD(length - 200, 8.775);
        I(length + 10);

        EnsureExist({GIGA, 2});
    }

    SetTime(0, 20);
    AvZ::MaidCheats::move();
    SetTime(500, 20);
    AvZ::MaidCheats::dancing();
    SetTime(1000, 20);
    AvZ::MaidCheats::move();
    SetTime(1200, 20);
    AvZ::MaidCheats::dancing();
    for (auto w : waves(20))
    {
        PP(282); // 投篮压9普283
        PP(318);
        DD(318);
        DD(318 + 110);
        PP(813 + 22);

        PP(600, 4.5);

        P(5500, 3, 9);
        SetIce(0, {{1, 6}, {2, 6}, {3, 6}, {4, 6}, {5, 6}, {5, 3}});
    }

    // 偷花
    SetTime(800 + 751, 4);
    Card(SUNFLOWER, 5, 3);
    SetTime(800 + 751, 5);
    Card(SUNFLOWER, 5, 6);
    SetTime(800 + 751, 14);
    Card(SUNFLOWER, 4, 6);
    // SetTime(800 + 751, 16);
    // Card(SUNFLOWER, 3, 6);//不赚不亏

    SetTime(800 + 751, 19);
    Card(SUNFLOWER, 3, 9);
    Delay(751 + 751 + 134);
    Card(SUNFLOWER, 1, 9);
    Delay(751);
    Card(SUNFLOWER, 2, 9);
    Delay(751);
    Card(SUNFLOWER, 4, 9);
    Delay(751);
    Card(SUNFLOWER, 5, 9);

    SetTime(1267, 19);
    Shovel(4, 6);
    // SetTime(5500 + 500 - 1, 20);
    SetTime(5998, 20);
    Shovel({{5, 3}, {5, 6}, {1, 9}, {2, 9}, {3, 9}, {4, 9}, {5, 9}});
}